﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;

namespace Lilium.Web.Mvc.Html
{
    public sealed class TagWriter : IDisposable
    {
        #region IDisposable Members

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion

        internal TagWriter(ViewContext viewContext, TagBuilder tagBuilder)
        {
            if (viewContext == null) throw new ArgumentNullException("viewContext");
            if (tagBuilder == null) throw new ArgumentNullException("tagBuilder");

            this.ViewContext = viewContext;
            this.TagBuilder = tagBuilder;

            this.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
        }

        private void Dispose(bool disposing)
        {
            if (!this.Disposed)
            {
                this.Disposed = true;
                this.ViewContext.Writer.Write(TagBuilder.ToString(TagRenderMode.EndTag));
            }
        }

        private bool Disposed;
        private ViewContext ViewContext;
        private TagBuilder TagBuilder;
    }
}
